<?php
function ListTypeModunle($select)
{
	$types=array(
			"module"	=>	"Module (default)",
            "contactus" => "Contact Us",
			"hotel"  	=>	"Hotels",
			"gallery"	=> "Gallery",
            "video"     => "Video",
			"home"		=> "Home page",
            "news"      => "News",
            "tour"      => "Tour",
            "tourtype"  => "Tour Type",
            "location"  => "Locations",
            "blog"      => "Blog",
            //"promotions"=>"Promotions",
            "sitemap"   => "Sitemap",
            "about"     => "About Us",
            "typicalworks"=>"Typical Works",
            'product'   => "Product",
            "faqs"      => "FAQs",
            "review"    => "Review",
            "promotion"=>"Promotion"
			);

	$options="";
	foreach($types as $key => $value)
	{
		if($select==$key)
		{
			$options.="<option value='$key' selected>$value</option>";
		}else
		{
			$options.="<option value='$key'>$value</option>";
		}
	}
	return $options;
}

function getIdParent($id)
{
global $dbsql,$language;
	$sql_select="select iParentId from tblcategory where iId='$id' and cLanguage='$language'";
	$rs=$dbsql->query($sql_select);
	$result=$dbsql->fetch_array($rs);
	return $result['iParentId'];
}

function ListContentByCatId($idCat,$status=-1)
{
global $dbsql,$language;

	$idCat         = intval($idCat);
	$sql_where     = " from tblcontent where iParentId=$idCat and cLanguage='$language' ";
    if($status>-1)
    {
        $sql_where.= " and iStatus = $status ";
    }

    $sql_select    = "select count(iId) as count ".$sql_where;
	$result        = $dbsql->query_fetch($sql_select);
	$count_all	   = $result['count'];
	list($limit_st,$limit_le)=Slit_Page($count_all);
	//-----------------------------------------------------
	$sql_select = "select * ".$sql_where." order by iOrder limit $limit_st,$limit_le";
	return $dbsql->load($sql_select);
}

function ListCatByParentId($idCat,$status=-1,$place=-1)
{
	global $dbsql,$language;
	$idCat         = intval($idCat);
	$sql_where     = " from tblcategory where iParentId=$idCat and cLanguage='$language' ";
    if($status>-1)
    {
        $sql_where.= " and iStatus = $status ";
    }
    if($place>-1)
    {
        $sql_where.= "and iPlace = $place ";
    }

    $sql_select    = "select count(iId) as count ".$sql_where;
	$result        = $dbsql->query_fetch($sql_select);
	$count_all	   = $result['count'];
	list($limit_st,$limit_le)=Slit_Page($count_all);
	//-----------------------------------------------------
	$sql_select = "select * ".$sql_where." order by iPlace,iOrder limit $limit_st,$limit_le";
	return $dbsql->load($sql_select);
}

function ListRootCatModunle($urlRoot="",$idParent=0,$padding=0)
{
global $id,$language,$dbsql;
	$idParent  = intval($idParent);
    $return    = '';
	//-----------------------------------------------------
	$sql_select = "select cName,iId,iHasHighLine from tblcategory where  iParentId=$idParent ";
    if($idParent==0)
	{
		$sql_select.=" and (cUrlString = '$urlRoot' or cType = '$urlRoot') ";
	}
	$sql_select.=" and cLanguage='$language' order by iOrder asc";

	$rs=$dbsql->query($sql_select);
	while($result = $dbsql->fetch_array($rs))
	{
		if($id==0)
		{
			$id=$result["iId"];
		}
		$child= ListRootCatModunle($urlRoot, $result["iId"],$padding+20);
		$return .="<div  style='padding-left:".$padding."px;'";
		if($result["iId"] == $id)
		{
			if($result['iHasHighLine']==1)
			{
				$type="highline";
			}
			$return .= " class='selected'";
		}
		$return.="><a ".(($idParent==0)?'style="color:#ff0000"':'')." href='?";
		if(!empty($urlRoot))$return.="module=".$urlRoot."&";
		$return.="id=".$result["iId"]."'>";
		$return.="&raquo;".$result["cName"]."</a></div>";
		$return.=$child;
	}
	return $return;
}

function ListOptionCategory($module,$select=0,$parentId=0,$padding=0)
{
    global $dbsql,$language,$module,$id,$act;

    $options    = '';
    $hasRoot    = true;
    if($parentId==-1)
    {
        $parentId   = 0;
        $hasRoot    = false;
    }
    $space=str_repeat("&nbsp;",$padding);

    $sql_select="select iId,iParentId,cName from tblcategory where iParentId=$parentId ";
    if($parentId==0)
	{
		$sql_select.=" and (cUrlString = '$module' or cType = '$module') ";
	}
    if($act=="edit")
    {
        $sql_select.=" and iId!=$id";
    }
    $sql_select.=" and cLanguage='$language' order by iOrder";
    $rs=$dbsql->query($sql_select);
    while($result=$dbsql->fetch_array($rs))
    {
        if($hasRoot==true)
        {
            if($select==$result['iId'])
            {
                $options.="<option value='".$result['iId']."' selected>".$space.$result['cName']."</option>";
            }else
            {
                $options.="<option value='".$result['iId']."'>".$space.$result['cName']."</option>";
            }
        }
        $options.=ListOptionCategory($module,$select,$result['iId'],$padding+7);
    }
    return $options;
}

function hasChildCategory($idp)
{
      global $dbsql;
      $idp=intval($idp);
      //-----------------------------------------------------
      $sql_select = "select iId from tblcategory where  iParentId = $idp";

      $rs=$dbsql->query($sql_select);
      $rows = $dbsql->num_rows($rs);
      if($rows>0)return true;
      return false;
}

function hasChildContent($idp)
{
      global $dbsql;
      $idp=intval($idp);
      //-----------------------------------------------------
      $sql_select = "select iId from tblcontent where  iParentId = $idp";

      $rs=$dbsql->query($sql_select);
      $rows = $dbsql->num_rows($rs);
      if($rows>0)return true;
      return false;
}

function isModule($id)
{
    global $dbsql,$module;

    $sql_select = "select iId from tblcategory where (cUrlString='$module' or cType='$module') and iId=$id and iParentId=0";
    $rs         = $dbsql->query($sql_select);
    if($dbsql->num_rows($rs)>0)
    {
        return true;
    }else
    {
        return false;
    }
}

function LoadCategoryById($id)
{
global $dbsql,$language;
	$id=intval($id);
	$sql_select = "SELECT * FROM tblcategory WHERE
			cLanguage='$language' and iId = ".$id." ";
	$rs=$dbsql->query($sql_select);
	$category = $dbsql->fetch_array($rs);
	return $category;
}

function InsertCategory($parentcategory,$urlstring,$name,$description,$Intro,$type,$metatitle,
    $metadescription,$metakeywords,$ImgIntro,$ImgBanner,$ImgBackground,$Status,$order,$HasHighLine=0)
{
	global $dbsql,$language;
	$HasHighLine	= intval($HasHighLine);
    $postdate   = date("Y-m-d");
    $poster     = getUserNamePost();
	//get order category same parent;

	$sql_insert = "INSERT INTO tblcategory
				(iParentId,cUrlString , cName,iOrder, cDescription,cIntro,cType,
				cMetaTitle, cMetaDescription, cMetaKeywords ,cImage, cBanner,cBackground,
				iStatus,iHasHighLine,dPostDate,cPoster,cLanguage)
			VALUE
				('$parentcategory','$urlstring','$name','$order','$description','$Intro','$type',
				'$metatitle','$metadescription','$metakeywords','$ImgIntro','$ImgBanner','$ImgBackground',
				'$Status','$HasHighLine','$postdate','$poster','$language')";

	return $dbsql->query($sql_insert);
}


function UpdateCategory($parentcategory,$urlstring,$categoryName,$description,$Intro,$type,$metatitle,
                        $metadescription,$metakeywords,$ImgIntro,$ImgBanner,$ImgBackground,
                        $Status,$order=-1,$HasHighLine=0)
{
global $dbsql,$language,$id;
	if(intval($id)==0)return false;
    $order      = intval($order);
	$sql_update = "UPDATE  tblcategory SET
				iParentId	 	= $parentcategory,
				cUrlString 		= '$urlstring',
				cName		 	= '$categoryName',
				cDescription 	= '$description',
				cIntro			='$Intro',
				".(!empty($type)? "cType	= '$type',": '')."
				iStatus			= '$Status',
				cMetaTitle 		= '$metatitle',
				cMetaDescription= '$metadescription',
				cMetaKeywords 	= '$metakeywords',
				cImage 			= '$ImgIntro',
				cBanner 		= '$ImgBanner',
                cBackground     = '$ImgBackground',
                ".(($order>0)? 'iOrder='.$order.',' : '')."
				iHasHighLine	= '$HasHighLine'

		WHERE  iId 	= $id";

	return $dbsql->query($sql_update);
}

function getIdCatByType($type)
{
    global $dbsql,$language;
    
    $sql_select = "select iId from tblcategory where cType='$type' and cLanguage='$language'";
    $result     = $dbsql->query_fetch($sql_select);
    return intval($result['iId']);
}
?>